package com.bqtv.sport.app

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.bqtv.sport.commen.action.TitleBarAction
import com.bqtv.sport.commen.action.ToastAction
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.TitleBar
import com.hjq.base.BaseActivity
import com.hjq.base.BaseFragment
import com.hjq.http.listener.OnHttpListener
import com.bqtv.sport.R
import com.bqtv.sport.commen.utils.ActivityManager
import com.hjq.base.app.LoginInfoUtils
import okhttp3.Call

abstract class AppFragment<T : ViewBinding, A : AppActivity<ViewBinding>> : BaseFragment<A>(),
    ToastAction, TitleBarAction,
    OnHttpListener<Any> {
    lateinit var binding: T

    /** 标题栏对象  */
    private var titleBar: TitleBar? = null
    private var immersionBar: ImmersionBar? = null

    override fun initLayout(inflater: LayoutInflater, container: ViewGroup?) :View?  {
        if (getViewBinding() != null) {
            binding = getViewBinding()!!
        }
       return super.initLayout(inflater,container)
    }

    abstract fun getViewBinding(): T?


    override fun getLayout(): View? {
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 设置标题栏点击监听
        if (getTitleBar() != null) {
            getTitleBar()?.setOnTitleBarListener(this)
        }
        if (isStatusBarEnabled()) {
            // 初始化沉浸式状态栏
            getStatusBarConfig().init()
            if (getTitleBar() != null) {
                // 设置标题栏沉浸
                ImmersionBar.setTitleBar(this, getTitleBar())
            }
        }
    }

    override fun getTitleBar(): TitleBar? {
        if (titleBar == null) {
            titleBar = obtainTitleBar(view as ViewGroup?)
        }
        return titleBar
    }


    /**
     * 是否使用沉浸式状态栏
     */
    protected open fun isStatusBarEnabled(): Boolean {
        return false
    }

    /**
     * 状态栏字体深色模式
     */
    open fun isStatusBarDarkFont(): Boolean {
        return true
    }

    /**
     * 获取状态栏沉浸的配置对象
     */
    open fun getStatusBarConfig(): ImmersionBar {
        if (immersionBar == null) {
            immersionBar = createStatusBarConfig()
        }
        return immersionBar!!
    }

    /**
     * 初始化沉浸式状态栏
     */
    open fun createStatusBarConfig(): ImmersionBar {
        return ImmersionBar.with(this) // 默认状态栏字体颜色为黑色
            .statusBarDarkFont(isStatusBarDarkFont()) // 指定导航栏背景颜色
            .navigationBarColor(R.color.white) // 状态栏字体和导航栏内容自动变色，必须指定状态栏颜色和导航栏颜色才可以自动变色
            .statusBarDarkFont(true)
            .autoDarkModeEnable(true, 0.2f)
    }

    /**
     * 当前加载对话框是否在显示中
     */
    open fun isShowDialog(): Boolean {
        val activity = getAttachActivity() ?: return false
        return activity.isShowDialog()
    }

    /**
     * 显示加载对话框
     */
    open fun showDialog() {
        val activity = getAttachActivity() ?: return
        activity.showDialog()
    }

    /**
     * 隐藏加载对话框
     */
    open fun hideDialog() {
        val activity = getAttachActivity() ?: return
        activity.hideDialog()
    }

    open fun loadHttp() {}

    /**
     * [OnHttpListener]
     */
    override fun onHttpStart(call: Call?) {
        showDialog()
    }

    override fun onHttpSuccess(result: Any) {

    }

    override fun onHttpFail(throwable: Throwable?) {

    }

    override fun onHttpEnd(call: Call?) {
        super.onHttpEnd(call)
        hideDialog()
    }
}